<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class ProController extends HomeController {

    /*分类管理*/
    // public function type(){}



    /*信息删除操作*/
    public function prodel(){
        $order= M('Product');
        $id= $_POST['id'];
        if($order ->where(array('id'=>$id))->delete()){
            $data = array('status'=>true,'msg' =>'操作成功');
            $this ->ajaxReturn($data,'json');
        }else{
            die($data = array('status'=>true,'msg' =>'操作成功'));
        }
    }


    /*产品编辑提交*/
    public function proAction(){
        $proAction = M('product');
        $id = $_POST['id'];
        $proAction->create();
        $res = $proAction ->where(array('id'=>$id))->save(); 
        if($res){
            $this ->redirect('pro/index');
        }else{
            $this ->error();
        }
    }

    /*产品编辑*/
    public function proedit(){
        $proedit =M('product');
        $id = $_GET['id'];
        $list = $proedit ->where(array('id'=>$id))->select();

        $this ->assign('list',$list);
        $this ->display('pro/proedit');
    }

	/*产品展示*/
    public function index(){
      $product = M('product');
      // $cid = $_GET['cid'];
      $cid = $_SESSION['home']['cid'];
      // var_dump($_GET);
      // $list = $product ->where(array('cid'=>$cid))->select();
      
      $count =$product->where(array('cid'=>$cid))->count();// 查询满足要求的总记录数
      $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
      $Page->setConfig('prev','上一页');
      $Page->setConfig('next','下一页');
      $Page->setConfig('last','末页');
      $Page->setConfig('first','首页');
      $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
      $show = $Page->show();// 分页显示输出
      
      // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
      $list =$product ->order('id')->where(array('cid'=>$cid))->limit($Page->firstRow.','.$Page->listRows)->select();
      // 
      // $this ->assign('cid',$cid);
      $this ->assign('page',$show);
      $this ->assign('list',$list);
      $this ->display('pro/index');

    }




    //添加产品页面
    public function proadd(){
        // $cid = $_GET['cid'];
        $cid = $_SESSION['home']['cid'];
        $Types = M('Type')->where(array('cid'=>$cid))->select();
        $tlist = $Types;
        // var_dump($_GET);
        $this->assign('tlist',$tlist);
        $Product = M('Product')->where(array('cid'=>$cid))->select();
        // $this->assign('cid',$cid);
        $this->assign('num',$num);
        $this->display('pro/proadd');
    }




    public function proincrease(){
    
        $data = $_POST;
        $Type = D('Type');
        $cid = $_SESSION['home']['cid'];
        $groupname = $Type->where('id='.$data['groupid'])->select();
        // echo '<pre>';
        // print_r($groupname);
        // exit;
        $data['groupname'] = $groupname[0]['title'];
        $keyword = $data['keyword'];
        $keyword = implode(',',$keyword);
        $data['keyword'] = $keyword;
        $data['cid'] = $cid;
        // echo '<pre>';
        // print_r($data);
        // exit;
        $Product = D('Product');
        $id = $Product->add($data);

        if($id){
            $this->redirect('Pro/index');
            $_SESSION['proid'] = $id; // 产品ID存入SEESSION
        } else {
            $this->error('新增失败');
        }
    }
        

    //所有产品
    // public function proindex(){

    
     


    //     $Product = D('Product');
    //     $list = $Product->where('cid='.$cid)->select();
    //     // echo '<pre>';
    //     // print_r($_SESSION);
    //     // print_r($list);
    //     // exit;

    //     // echo "<pre>";
        
    //     $this->assign('list',$list);
    //     $this->display();
    // }

    /**/

    /*分类删除*/

    public function typedel(){
        $order= M('type');
        $id= $_POST['id'];
        if($order ->where(array('id'=>$id))->delete()){
            $data = array('status'=>true,'msg' =>'操作成功');
            $this ->ajaxReturn($data,'json');
        }else{
            die($data = array('status'=>true,'msg' =>'操作成功'));
        }
    }


    //分类管理
    public function typeindex(){

        
        $cid = $_SESSION['home']['cid'];
        $Type = M('Type');
        $list = $Type->field(array('id','title','sort','cid'))->where(array('cid'=>$cid))->select();



        $count =$Type->where(array('cid'=>$cid))->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
      
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list =$Type ->order('id')->where(array('cid'=>$cid))->limit($Page->firstRow.','.$Page->listRows)->select();

        // var_dump($list);
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->display('pro/typeindex');
    }
    
    public function tyindex(){

        // var_dump($_SESSION);
        $this ->display('pro/tyindex');
    }

    /*分类编辑*/

    public function typeedit(){
        $typeedit = M('type');
        $id = $_GET['id'];
        $list = $typeedit ->field(array('id','title','sort','cid'))->where(array('id'=>$id))->select();

        $this ->assign('list',$list);
        $this ->display('pro/typeedit');
    }

    public function typeAction(){
        $typeAction = M('type');
        $id = $_POST['id'];
        // var_dump($_POST);
        $typeAction ->create();
        $res = $typeAction ->where(array('id'=>$id)) ->save();
        // var_dump($res);
        if($res){
            $this ->redirect('pro/typeindex');
        }else{
            $this ->error();
        }
    }


    // 分类添加
    public function typeadd(){
        // var_dump($_POST);
    // echo 1;
        $type = M('type');
        $_POST['cid'] = $_SESSION['home']['cid'];

        $type ->create();

        $res = $type ->add();

        if($res){


            // echo 1;
            $this ->redirect('pro/typeindex');
        }else{
            $this ->error();
        }

    }



    public function prodelmore(){
        $list = $_POST['prolist'];
        // print_r($list);

        $list = implode(',',$list);


        
        $Product = D('Product');

        $sql = $Product->query('select path from onethink_product where id in('.$list.')');

        $path = $sql;
        foreach($path as $k=>$v){
            // echo $v['path'];
            unlink($_SERVER['DOCUMENT_ROOT'].$v['path']); //删除多张图
        }
        // echo'<pre>';
        // print_r($path);

        
        $result = $Product->execute('delete from onethink_product where id in('.$list.')');

    }

    // -----------------------------------------------------------

    public function typedelmore(){
        
        $list = $_POST['typelist'];
        $list = implode(',',$list);

        $Product = D('Product');
        $res = $Product->query('select id from onethink_product where groupid in('.$list.')');
        $res = count($res);
        // echo '<pre>';
        // echo $res;
        // exit;
        if($res>0){
            die();
        }else{
            $Type = D('Type');
            $r = $Type->query('select title from onethink_type where id in('.$list.')');
            $result = $Type->execute('delete from onethink_type where id in('.$list.')');
            print_r($r); 
        }

// --------------------------------------------------------------       

    }

    public function import(){
        $this->display('import.html');
    }


    public function session(){
         $_SESSION = $_SESSION;
         $id = $_POST;
         $_SESSION['proid'] = $id;
    }








    /* 文件上传 */
    public function upload(){

        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
        /* 调用文件上传组件上传文件 */
        $File = D('Home/File');
        $file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
        $info = $Product->upload(
            $_FILES,
            C('DOWNLOAD_UPLOAD'),
            C('DOWNLOAD_UPLOAD_DRIVER'),
            C("UPLOAD_{$file_driver}_CONFIG")
        );

        /* 记录附件信息 */
        if($info){
            $return['data'] = think_encrypt(json_encode($info['download']));
            $return['info'] = $info['download']['name'];
        } else {
            $return['status'] = 0;
            $return['info']   = $Product->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }

    /* 下载文件 */
    public function download($id = null){
        if(empty($id) || !is_numeric($id)){
            $this->error('参数错误！');
        }

        $logic = D('Download', 'Logic');
        if(!$logic->download($id)){
            $this->error($logic->getError());
        }

    }

    /**
     * 上传图片
     * @author huajie <banhuajie@163.com>
     */
    public function uploadPicture(){
        //TODO: 用户登录检测
        echo "string";
        /* 返回标准数据 */
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

        /* 调用文件上传组件上传文件 */
        $Product = D('Home/Product');
        // echo '<pre>';
        // print_r($Product);
        // exit;
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Product->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器

        /* 记录图片信息 */
        if($info){
            $return['status'] = 1;
            $return = array_merge($info['download'], $return);
        } else {
            $return['status'] = 0;
            $return['info']   = $Product->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);

    }


}